package com.bouvet.wave;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Blip;
import com.google.wave.api.Event;
import com.google.wave.api.EventType;
import com.google.wave.api.Gadget;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;

public class BouvetWaveServlet  extends AbstractRobotServlet {
	private static final Logger log = Logger.getLogger(BouvetWaveServlet.class
			.getName());
	private final static String BOT_ID="bouvetwave@appspot.com";
	private final static String GADGET_CUSTOMER_URL="http://bouvetwave.appspot.com/gadget_customer.xml";
	private final static String GADGET_CUSTOMER_STATE_ID="customer_state";
	
	private final static String GADGET_PROVIDER_URL="http://bouvetwave.appspot.com/gadget_provider.xml";
	private final static String GADGET_PROVIDER_STATE_ID="provider_state";
	
	private final static String PARTICIPANT_KAM="dparnas-test@wavesandbox.com";
	
	public void processEvents(RobotMessageBundle bundle) {
		log.info("In processEvents");
		Wavelet wavelet = bundle.getWavelet();

		if (bundle.wasSelfAdded()) {
			doOnSelfAdded(wavelet);
		}
		for (Event e : bundle.getEvents()) {
			log.info("Got event " + e);
			if (e.getType() == EventType.BLIP_SUBMITTED && modifiedByOtherParticipant(e) ){
				Blip blipOrg = e.getBlip();
				
				Blip blipReply = blipOrg.createChild();
				TextView textView = blipReply.getDocument();
				textView.append(e.getModifiedBy() + " modified the wave");
				
			}
		}
	}
	/**
	 * Method called when the robot is added to the wave
	 * It results in the following actions:
	 * 1. KAM is added to original wavelet 
	 * 2. A confirmation to the customer
	 * 3. A gadget is added for the customer to follow status
	 * 4. A wavelet only visible to the provider is created
	 * 
	 * @param wavelet
	 */
	private void doOnSelfAdded(Wavelet wavelet){
		wavelet.addParticipant(PARTICIPANT_KAM);
		createConfirmationToCustomer(wavelet);

		createProviderWavelet(wavelet);

		
	}
	
	/**
	 * Creates the initial confirmation to the customer 
	 * and gadget for keeping track of status
	 * 
	 * @param wavelet
	 */
	private void createConfirmationToCustomer(Wavelet wavelet) {
		//We add reply directly under the initial blip
		Blip blipCustomer = wavelet.getRootBlip().createChild();
		TextView textViewCustomer = blipCustomer.getDocument();
		textViewCustomer.append("Thank you for your request. Our Key account manager has been added to the wave and we'll respond as quickly as possible. The gadget below will keep you informed on the current status of this enquiry");
		
		//Add the gadget
		Gadget gadgetCustomer = new Gadget(GADGET_CUSTOMER_URL);
		gadgetCustomer.setField(GADGET_CUSTOMER_STATE_ID, "Initial state");
		textViewCustomer.appendElement(gadgetCustomer);
	
	}
	
	/**
	 * Creates the internal wavelet not visible to the customer
	 * 
	 * @param wavelet The wavelet in which the customer created the request
	 */
	private void createProviderWavelet(Wavelet wavelet) {
		//add new internal wavelet by removing creator of wave
		String creator= wavelet.getCreator();
		List<String> listParticipants = wavelet.getParticipants();
		if (listParticipants.contains(creator)){
			log.info("Removing creator " + creator);
			listParticipants.remove(creator);
		}
		//make sure the KAM is added
		listParticipants.add(PARTICIPANT_KAM);
		
		//create the wavelet
		Wavelet waveletProvider = wavelet.createWavelet(listParticipants, "INTERNAL");
		
		//add the initial content to the provide wavelet
		Blip blipProvider = waveletProvider.getRootBlip();
		TextView textViewProvider = blipProvider.getDocument();
		textViewProvider.append("INTERNAL: Request xxx");
		
		textViewProvider.append("Please fill out the form in the gadget below:");
		//Add the gadget
		Gadget gadgetCustomer = new Gadget(GADGET_PROVIDER_URL);
		gadgetCustomer.setField(GADGET_PROVIDER_STATE_ID, "Initial state");
		textViewProvider.appendElement(gadgetCustomer);
		
	}
	
	
	private boolean modifiedByOtherParticipant(Event e){
		return !e.getModifiedBy().equals(BOT_ID) && !e.getModifiedBy().endsWith("@gwave.com");
	}
	
}
